﻿#pragma warning disable 1591

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace TotalFreedomBST
{
    public class TabNavSection: BstSection
    {
        private HtmlHelper _htmlHelper;

        public TabNav TabNav { get; set; }

        public TabNavSection(HtmlHelper htmlHelper, TabNav tabNav, string id, TabNavOptions options,  RouteValueDictionary htmlAttributes)
            : base(htmlHelper.ViewContext, "</div>\r\n")
        {
            if (tabNav != null)
            {
                TabNav = tabNav;
                if (htmlAttributes != null)
                    TabNav.HtmlAttributes = htmlAttributes;
            }
            else
                TabNav = new TabNav(_htmlHelper, options, id, htmlAttributes);            

            _htmlHelper = htmlHelper;
        }

        private string TabPaneList(TabPane parentTabPane = null)
        {
            IEnumerable<TabPane> tabPanes = TabNav.TabPanes.Where(w => w.ParentTabPane == parentTabPane);
            string s = "";

            if (tabPanes.Any())
            {
                s = "<ul class=\"" + (parentTabPane == null ? "nav nav-tabs" : "dropdown-menu") + "\">\r\n";
                foreach (TabPane tabPane in tabPanes)
                {
                    s += "<li";
                    if (((tabPane.Options & TabPaneOptions.Active) == TabPaneOptions.Active))
                        s += " class=\"active\"";

                    s += "><a href=\"#" + tabPane.Id + "\" data-toggle=\"tab\">" + tabPane.Caption.ToHtmlString();

                    if ((tabPane.Options & TabPaneOptions.Caret) == TabPaneOptions.Caret)
                        s += "<b class=\"caret\"></b>";

                    s += "</a>";

                    s += TabPaneList(tabPane);

                    s += "</li>\r\n";
                }
                s += "</ul>\r\n";
            }

            return s;
        }

        public override string StartHtml()
        {
            string paneList = TabNav.Options == TabNavOptions.Below ? "" : TabPaneList();

            string attrClass = "tabbable";
            string s = "<div ";

            if (TabNav.Options == TabNavOptions.Below)
                attrClass += " tabs-below";
            else if (TabNav.Options == TabNavOptions.Left)
                attrClass += " tabs-left";
            else if (TabNav.Options == TabNavOptions.Right)
                attrClass += " tabs-right";

            s += BstHelperExtensions.ValuePlusAttribute("class", attrClass, TabNav.HtmlAttributes) + BstHelperExtensions.AttributeOrDefault(TabNav.HtmlAttributes, "id", TabNav.Id)  + ">\r\n" + paneList + "<div class=\"tab-content\">";

            return s;
        }

        protected override string EndHtml()
        {
            string s = "</div>\r\n";

            if (TabNav.Options == TabNavOptions.Below)
                s += TabPaneList();

            return s;
        }

        public TabPaneSection TabPane(TabPane tabPane, object htmlAttributes = null)
        {
            TabPaneSection tabPaneSection = new TabPaneSection(this.TabNav, _htmlHelper.ViewContext, tabPane);

            _htmlHelper.ViewContext.Writer.Write(tabPaneSection.StartHtml());

            return tabPaneSection;
        }

        public TabPane CreateTabPane(string id = null)
        {
            return TabNav.CreateTabPane(id);
        }
    }

    public enum TabNavOptions
    {
        None = 0,
        Above = 1,
        Below = 2,
        Left = 3,
        Right = 4,
        Fade = 32
    }

    public class TabNav: ITab
    {
        public HtmlHelper HtmlHelper { get; private set; }
        public string Id { get; set; }
        public readonly List<TabPane> TabPanes = new List<TabPane>();
        public TabNavOptions Options { get;  set; }
        public RouteValueDictionary HtmlAttributes { get; set; }

        public TabNav(HtmlHelper htmlHelper, TabNavOptions options, string id, RouteValueDictionary htmlAttributes)
        {
            HtmlHelper = htmlHelper;
            Options = options;
            HtmlAttributes = htmlAttributes;

            if (String.IsNullOrWhiteSpace(id))
                Id = System.Guid.NewGuid().ToString("N");
            else
                Id = id;
        }

        public bool IsFade()
        {
            return (Options & TabNavOptions.Fade) == TabNavOptions.Fade;
        }

        public TabPane CreateTabPane(string caption, TabPaneOptions options = TabPaneOptions.None, TabPane parentTabPane = null, string id = null, object htmlAttributes = null)
        {
            return CreateTabPane(new MvcHtmlString(HttpUtility.HtmlEncode(caption)), options, parentTabPane, id, htmlAttributes);
        }

        public TabPane CreateTabPane(IHtmlString caption, TabPaneOptions options = TabPaneOptions.None, TabPane parentTabPane = null, string id = null, object htmlAttributes = null)
        {
            RouteValueDictionary attrs = null;
            if (htmlAttributes != null)
                attrs = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);

            TabPane tabPane = new TabPane(this, caption, options, id, attrs);
            tabPane.ParentTabPane = parentTabPane;
            TabPanes.Add(tabPane);

            return tabPane;
        }
    }
}
